from django.shortcuts import render
from django.contrib.auth.hashers import make_password,check_password
from rest_framework.views import APIView
from rest_framework.response import Response
from . import models
from .serializer import *
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
from django.conf import settings
import re
# Create your views here.
user_token=Serializer(settings.SECRET_KEY)


class Register(APIView):
    def post(self,request):
        phone=request.POST.get('phone')
        name=request.POST.get('username')
        word=request.POST.get('password')
        word1=request.POST.get('password1')
        img=request.POST.get('img')
        if not re.match(r'1[35678]\d{9}$',phone):
            return Response({'msg':'电话号码错误','code':1001})
        user=models.User.objects.filter(username=name).first()
        if user:
            return Response({'msg':'用户已存在','code':1000})
        if word==word1:
            data={
                'username':name,'password':make_password(word),'phone':phone,'img':img
            }
            obj=UserSerializer(data=data)
            if obj.is_valid():
                obj.save()
                return Response({'msg': '注册成功', 'code': 1000})
            return Response({'msg': '注册失败', 'code': 1001})
        return Response({'msg': '两次密码不一致', 'code': 1002})

    def get(self,request):
        user=models.User.objects.all()
        obj=UsersSerializer(user,many=True)
        return Response(obj.data)

class Login(APIView):
    def post(self,request):
        name = request.POST.get('username')
        word = request.POST.get('password')
        user=models.User.objects.filter(username=name).first()
        if user:
            if check_password(word,user.password):
                user_info={'user_id':user.id}
                token=user_token.dumps(user_info).decode()
                return Response({'msg':'登录成功','code':1000,'token':token})
            else:
                return Response({'msg': '密码或用户名错误', 'code': 1001})
        else:
            return Response({'msg': '登录失败', 'code': 1002})


class AddUser(APIView):
    def post(self,request):
        user=request.POST.get('user')
        user_obj=models.Users.objects.filter(user=user).first()
        if user_obj:
            return Response({'msg': '信息存在', 'code': 1001})
        user_serializer=UsersSerializer(data=request.data)
        if user_serializer.is_valid():
            user_serializer.save()
            return Response({'msg': '添加成功', 'code': 1000})
        else:
            print(user_serializer.errors)
            return Response({'msg': '添加失败', 'code': 1002})

class UpdateUser(APIView):
    def post(self,request):
        info=request.data
        token=request.data.get('user')
        user_info=user_token.loads(token)
        user_id=user_info.get('user_id')
        obj=models.Users.objects.filter(user=user_id).first()
        if obj:
            obj.age=info.get('age')
            obj.detailed_address=info.get('detailed_address')
            obj.gender=info.get('gender')
            obj.address=info.get('address')
            obj.email=info.get('email')
            obj.save()
        return Response({'msg':'修改成功','code':1000})
